package pers.qianyu.core.http;

import io.swagger.annotations.*;
import lombok.*;
import lombok.experimental.*;
import pers.qianyu.core.exception.*;

import java.time.*;
import java.time.format.*;

/**
 * Lamb 项目统一接口返回值
 *
 * @param <T> 返回数据类型
 * @author mizzle_rain
 */
@Data
@Accessors(chain = true)
@ApiModel("通用接口返回对象")
public class HttpResult<T> {
    @ApiModelProperty(required = true, notes = "结果码", example = "0")
    private int code;
    @ApiModelProperty(required = true, notes = "返回信息", example = "操作成功")
    private String message;
    @ApiModelProperty(required = true, notes = "返回数据", example = "{\"id\":2001}")
    private T data;
    @ApiModelProperty(required = true, notes = "时间戳", example = "2020-10-08 09:07:34")
    private String timestamp;

    {
        timestamp = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static HttpResult error(Integer code, String message) {
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(code);
        httpResult.setMessage(message);
        return httpResult;
    }

    public static HttpResult error(String message) {
        return error(5000, message);
    }

    public static HttpResult error(ILambErrorCodeEnum errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }

    public static HttpResult error(LambBaseException e) {
        return error(e.getCode(), e.getMessage());
    }

    public static HttpResult ok() {
        HttpResult httpResult = new HttpResult();
        httpResult.setMessage("请求成功");
        return httpResult;
    }

    public static <T> HttpResult<T> ok(T t) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setMessage("请求成功");
        httpResult.setData(t);
        return httpResult;
    }
}